package com.situ.mall.product.listener;

import com.alibaba.excel.context.AnalysisContext;

import com.alibaba.excel.event.AnalysisEventListener;
import com.situ.mall.pojo.entity.product.Category;
import com.situ.mall.pojo.entity.product.Product;
import com.situ.mall.product.mapper.CategoryMapper;
import com.situ.mall.product.mapper.ProductMapper;
import com.situ.mall.product.pojo.vo.ProductExcelVO;
import org.springframework.beans.BeanUtils;

public class ProductExcelListener extends AnalysisEventListener<ProductExcelVO> {
	
	private ProductMapper productMapper;
	private CategoryMapper categoryMapper;
	
	public ProductExcelListener(ProductMapper productMapper, CategoryMapper categoryMapper) {
		this.productMapper = productMapper;
		this.categoryMapper = categoryMapper;
	}
	
	@Override
	public void invoke(ProductExcelVO productExcelVO, AnalysisContext analysisContext) {
		Product product=new Product();
		BeanUtils.copyProperties(productExcelVO,product);
		if(productExcelVO.getCategory1Name()!=null && productExcelVO.getCategory1Name()!=""){
			Category category1=categoryMapper.selectByName(productExcelVO.getCategory1Name());
			product.setCategory1Id(category1.getId());
		}
		if(productExcelVO.getCategory2Name()!=null && productExcelVO.getCategory2Name()!=""){
			Category category2=categoryMapper.selectByName(productExcelVO.getCategory2Name());
			product.setCategory2Id(category2.getId());
		}
		if(productExcelVO.getCategory3Name()!=null && productExcelVO.getCategory3Name()!=""){
			Category category3=categoryMapper.selectByName(productExcelVO.getCategory3Name());
			product.setCategory3Id(category3.getId());
		}
		product.setId(null);
		productMapper.insert(product);
	}
	
	@Override
	public void doAfterAllAnalysed(AnalysisContext analysisContext) {
	
	}
}
